package com.fzsf.drawingboard.bluetooth.iskn;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.fzsf.drawingboard.app.MyApplication;
import com.fzsf.drawingboard.bluetooth.IFZWritingPadCallBackListener;
import com.iskn.isknApi.Event;
import com.iskn.isknApi.Listener;
import com.iskn.isknApi.Rect;
import com.iskn.isknApi.Size;
import com.iskn.isknApi.SlateManager;
import com.iskn.isknApi.Vector3D;
import com.iskn.isknApi.bleScanner.BLE_Scan_Listener;
import com.iskn.isknApi.bleScanner.BleScanner;
import com.iskn.isknApi.bleScanner.BleScanner_Event;
import com.iskn.isknApi.events.EventDescription;
import com.iskn.isknApi.events.EventHardware;
import com.iskn.isknApi.events.EventLocQuality;
import com.iskn.isknApi.events.EventPen3D;

import static com.fzsf.drawingboard.bluetooth.FZWritingPadConstant.ConnectState.STATE_CONNECED;
import static com.fzsf.drawingboard.bluetooth.FZWritingPadConstant.ConnectState.STATE_DISCONNECED;

/**
 * Created by Taijl on 2018/3/1.
 */

public class FZWritingPad_ISKNHelper implements Listener {
    public static FZWritingPad_ISKNHelper instance = new FZWritingPad_ISKNHelper();
    private SlateManager slateManager;
    private IFZWritingPadCallBackListener callBackListener;

    private FZWritingPad_ISKNHelper() {
    }

    public void setFZWritingPadCallBackListener(IFZWritingPadCallBackListener listener) {
        callBackListener = listener;
    }


    public static FZWritingPad_ISKNHelper getInstance() {
        return instance;
    }

    /**
     * 连接设备
     */
    public void connectDevice(final BluetoothDevice device) {
        if (null == slateManager) {
            slateManager = new SlateManager(MyApplication.getInstance());
        }
        slateManager.registerListener(this);
        slateManager.connect(device);
    }

    public void startScan() {
        BleScanner bLEScanner = new BleScanner(MyApplication.getInstance(), new BLE_Scan_Listener() {
            @Override
            public void newDeviceFound(BluetoothDevice bluetoothDevice) {
                if (null!=callBackListener){
                    callBackListener.onDeviceFound(bluetoothDevice);
                }
            }

            @Override
            public void notify(BleScanner_Event bleScanner_event, String s) {
            }

            @Override
            public void scanFailed(int i) {
                if (null!=callBackListener){
                    callBackListener.onLeScanEnd();
                }
            }

            @Override
            public void scanFinished() {
                if (null!=callBackListener){
                    callBackListener.onLeScanEnd();
                }
            }
        });

        try {
            bLEScanner.startScan();
        } catch (Exception e) {
            e.printStackTrace();
            if (null!=callBackListener){
                callBackListener.onLeScanEnd();
            }
        }
    }

    public void disConnect() {
        if (null != slateManager) {
            slateManager.disconnect();
        }
    }

    public boolean isConnected() {
        if (null != slateManager) {
            return slateManager.isConnected();
        }
        return false;
    }

    /**
     * 连接状态的变化监听
     */
    @Override
    public void connectionStatusChanged(boolean b) {
        if (b && null != slateManager) {
            slateManager.request(SlateManager.SingleRequestBlockType.REQ_DESCRIPTION);
            slateManager.subscribe(SlateManager.AutoBlockType.AUTO_STATUS.type() |
                    SlateManager.AutoBlockType.AUTO_PEN_3D.type() |
                    SlateManager.AutoBlockType.AUTO_HARDWARE_EVENTS.type() |
                    SlateManager.AutoBlockType.AUTO_SOFTWARE_EVENTS.type());
        }

        if (null != callBackListener) {
            callBackListener.onConnectStateChange(b ? STATE_CONNECED : STATE_DISCONNECED);
        }
    }

    /**
     * Slate 事件回调通知
     *
     * @param event
     * @param i
     */
    @Override
    public void processEvent(Event event, int i) {
        if (event == null) {
            return;
        }
        switch (event.getType()) {
            case EVT_DESCRIPTION:
                EventDescription evt = (EventDescription) event;
                Rect activeZone = evt.getActiveZone();
                Size SlateSize = evt.getSlateSize();
                String name = evt.getDeviceName();

                Log.i("FZWritingPad_ISKNHelper", "Connected to " + name + "\n");
                Log.i("FZWritingPad_ISKNHelper", "Firmware version :" + evt.getFirmwareVersion() + "\n");
                Log.i("FZWritingPad_ISKNHelper", "Slate Size (mm): " + "  width : " + SlateSize.getWidth() + " height : " + SlateSize.getHeight() + "\n");
                Log.i("FZWritingPad_ISKNHelper", "Active Zone (mm): " + " left : " + activeZone.getLeft() + " top : " + activeZone.getTop() + " width : " + activeZone.getWidth() + " height : " + activeZone.getHeight() + "\n");
                break;
            case EVT_PEN_2D:
                break;
            case EVT_PEN_3D:
                EventPen3D evt3D = (EventPen3D) event;
                Vector3D vec = evt3D.getPosition();
                if (evt3D.Touch()) {
                    Log.i("FZWritingPad_ISKNHelper", "Pen 3D : Touch " + "X: " + vec.getPosX() + ", Y: " + vec.getPosY() + ", Z: " + vec.getPosZ() + "\n");
                    if (null != callBackListener)
                        callBackListener.onWriting((int) ((vec.getPosX() - 17.0) * 100), (int) ((vec.getPosY() - 37.8) * 100), (int) vec.getPosZ() * 1000, System.currentTimeMillis());
                } else {
                    Log.i("FZWritingPad_ISKNHelper", "Pen 3D : UnTouch" + "X: " + vec.getPosX() + ", Y: " + vec.getPosY() + ", Z: " + vec.getPosZ() + "\n");
                    if (null != callBackListener)
                        callBackListener.onWriting((int) ((vec.getPosX() - 17.0) * 100), (int) ((vec.getPosY() - 37.8) * 100), 0, System.currentTimeMillis());
                }
                break;
            case EVT_HARDWARE:
                EventHardware evtHardware = (EventHardware) event;
                switch (evtHardware.getHardwareEventType()) {
                    case HE_BUTTON1_PRESSED:
                        Log.i("FZWritingPad_ISKNHelper", "Button 1 pressed\n");
                        if (null != callBackListener)
                            callBackListener.onButtonClick("1");
                        break;
                    case HE_BUTTON2_PRESSED:
                        Log.i("FZWritingPad_ISKNHelper", "Button 2 pressed\n");
                        if (null != callBackListener)
                            callBackListener.onButtonClick("2");
                        break;
                    case HE_BUTTON1_LONGPRESS:
                        Log.i("FZWritingPad_ISKNHelper", "Button 1 long pressed\n");
                        break;
                    case HE_BUTTON2_LONGPRESS:
                        Log.i("FZWritingPad_ISKNHelper", "Button 2 long pressed\n");
                        break;
                    case HE_UNKNOWN:
                        Log.i("FZWritingPad_ISKNHelper", "Unknown hardware event\n");
                        break;
                    case HE_REFRESH_DONE:
                        Log.i("FZWritingPad_ISKNHelper", "Slate refresh is done\n");
                        break;
                }
                break;
            case EVT_LOC_QUALITY:
                Log.i("disturbance", "" + ((EventLocQuality) event).getDisturbanceLevel());
                Log.i("status", "" + ((EventLocQuality) event).getLocStatus());
                break;

        }

    }

    public void switchWorkMode(int workMode) {
        if (null != callBackListener) {
            Log.d("FZWritingPad_ISKNHelper", "switchWorkMode");
            callBackListener.onWorkModeChange(workMode);
        }
    }
}
